package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.ApUserConstants;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApCollectionBehavior;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.behavior.vos.ApLoadArticleBehaviorVO;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @Author XHui
 * @Since 2024/3/3 17:51
 * @Version 1.0
 */
@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {

    @Resource
    private ApBehaviorEntryService apBehaviorEntryService;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Resource
    private  RedisTemplate redisTemplate;

    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {

        // 1. 查询文章和作者的关联关系
        Long articleId = dto.getArticleId();
        Integer equipmentId = dto.getEquipmentId();
        ApLikesBehavior apLikesBehavior = apBehaviorEntryService.findBehaviorTraceByEntryIdAndArticleId(
                articleId,
                equipmentId,
                ApLikesBehavior.class
        );

        ApUnlikesBehavior apUnlikesBehavior = apBehaviorEntryService.findBehaviorTraceByEntryIdAndArticleId(
                articleId,
                equipmentId,
                ApUnlikesBehavior.class
        );

        ApCollectionBehavior apCollectionBehavior = apBehaviorEntryService.findBehaviorTraceByEntryIdAndArticleId(
                articleId,
                equipmentId,
                ApCollectionBehavior.class
        );

        // 2. 创建返回核心数据
        ApLoadArticleBehaviorVO loadArticleBehaviorVO = new ApLoadArticleBehaviorVO();
        if (apLikesBehavior != null) {
            loadArticleBehaviorVO.setLike(true);
        }
        if (apUnlikesBehavior != null) {
            loadArticleBehaviorVO.setUnlike(true);
        }
        if (apCollectionBehavior != null) {
            loadArticleBehaviorVO.setCollection(true);
        }
        ApUser user = AppThreadLocalUtils.getUser();
        Integer userId = user.getId();
        String followKey = UserRelationConstants.FOLLOW_LIST + userId;
        ZSetOperations<String, String> zSet = stringRedisTemplate.opsForZSet();
        Double score = zSet.score(followKey,
                dto.getAuthorApUserId().toString());
        if(score != null) {
            loadArticleBehaviorVO.setFollow(true);
        }
        return ResponseResult.okResult(loadArticleBehaviorVO);
    }
}
